﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using BakalarskaPraca.Generation.Data;
using ColladaDotNet.Geometry;

namespace BakalarskaPraca.Forms.Controls
{
    public partial class TargetGroupControl : UserControl
    {
        private static readonly ColorDialog ColorDialog = new ColorDialog();
        private static int _nameIterator;
        public Color SelectionColor { get; private set; }
        public MarkedPolygonGroup PolygonGroup;
        private bool _selected;
        private readonly Action<TargetGroupControl> _selectionCallback;
        private readonly Action _redrawCallback;

        private Color _markColor;
        public Color MarkColor
        {
            get { return _markColor; }
            set { _markColor = Color.FromArgb(64, value); }
        }

        public TargetGroupControl(List<Polygon> polygons, Action<TargetGroupControl> selectionCallback, Action redrawCallback)
        {
            Random rand = new Random();
            InitializeComponent();
            this.TargetName.Text = "Target_" + _nameIterator++;
            this.TargetNameEdit.Text = this.TargetName.Text;
            this.SelectionColor = Color.FromArgb(rand.Next(256),
                                       rand.Next(256),
                                       rand.Next(256));
            this.ColorPreview.BackColor = this.SelectionColor;
            this.PolygonGroup = new MarkedPolygonGroup(polygons.Select(polygon => new HumidityPolygon(polygon)));
            this._selectionCallback = selectionCallback;
            this._redrawCallback = redrawCallback;
            this.MarkColor = this.SelectionColor;
            this.PolygonGroup.Name = this.TargetName.Text;
        }

        private void TargetNameEditLeave(object sender, EventArgs e)
        {
            this.TargetName.Text = this.TargetNameEdit.Text;
            this.PolygonGroup.Name = this.TargetName.Text;
            this.TargetNameEdit.Hide();
            this.TargetName.Show();
        }
        
        private void ColorPreviewClick(object sender, EventArgs e)
        {
            ColorDialog.Color = this.SelectionColor;
            if (ColorDialog.ShowDialog() == DialogResult.OK)
            {
                this.SelectionColor = ColorDialog.Color;
                this.ColorPreview.BackColor = this.SelectionColor;
                this.MarkColor = this.SelectionColor;
                this._redrawCallback();
            }
        }

        private void DeleteBtnClick(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
            this._redrawCallback();
        }

        private void TargetNameDoubleClick(object sender, EventArgs e)
        {
            this.TargetNameEdit.Text = this.TargetName.Text;
            this.TargetName.Hide();
            this.TargetNameEdit.Show();
            this.TargetNameEdit.Focus();
        }

        public void SelectControl(bool select = true)
        {
            this._selected = select;
            if (this._selected)
            {
                this.BackColor = SystemColors.Highlight;
            }
            else
            {
                this.BackColor = SystemColors.Control;
            }
        }

        private void TargetGroupControlClick(object sender, EventArgs e)
        {
            this._selectionCallback(this);
        }

        private void TargetNameClick(object sender, EventArgs e)
        {
            this._selectionCallback(this);
        }
    }
}
